/**
 * jQuery EasyUI 1.4
 * 
 * Copyright (c) 2009-2014 www.jeasyui.com. All rights reserved.
 *
 * Licensed under the GPL license: http://www.gnu.org/licenses/gpl.txt
 * To use it on other terms please contact us at info@jeasyui.com
 *
 */
(function ($) {
    function initialize(target) {
        $(target).addClass("textbox-f").hide();
        var tb = $(
            "<span class=\"textbox\">" +
            "<input class=\"textbox-text\" autocomplete=\"off\">" +
            "<span class=\"textbox-addon\"><span class=\"textbox-icon\"></span></span>" +
            "<input type=\"hidden\" class=\"textbox-value\">" + "</span>"
            ).insertAfter(target);
        var name = $(target).attr("name");
        if (name) {
            tb.find("input.textbox-value").attr("name", name);
            $(target).removeAttr("name").attr("textboxName", name);
        }
        tb.bind("_resize", function (e, data) {
            if ($(this).hasClass("easyui-fluid") || data) {
                setSize(target);
            }
            return false;
        });
        return tb;
    };
    function initIcons(target) {
        var state = $.data(target, "textbox");
        var opts = state.options;
        var tb = state.textbox;
        tb.find(".textbox-text").remove();
        if (opts.multiline) {
            $("<textarea class=\"textbox-text\" autocomplete=\"off\"></textarea>").prependTo(tb);
        } else {
            $("<input type=\"" + opts.type + "\" class=\"textbox-text\" autocomplete=\"off\">").prependTo(tb);
        }
        tb.find(".textbox-addon").remove();
        var bb = opts.icons ? $.extend(true, [], opts.icons) : [];
        if (opts.iconCls) {
            bb.push({ iconCls: opts.iconCls, disabled: true });
        }
        if (bb.length) {
            var bc = $("<span class=\"textbox-addon\"></span>").prependTo(tb);
            bc.addClass("textbox-addon-" + opts.iconAlign);
            for (var i = 0; i < bb.length; i++) {
                bc.append("<a href=\"javascript:void(0)\" class=\"textbox-icon " + bb[i].iconCls + "\" icon-index=\"" + i + "\"></a>");
            }
        }
        tb.find(".textbox-button").remove();
        if (opts.buttonText || opts.buttonIcon) {
            var button = $("<a href=\"javascript:void(0)\" class=\"textbox-button\"></a>").prependTo(tb);
            button.addClass("textbox-button-" + opts.buttonAlign).linkbutton({
                text: opts.buttonText, iconCls: opts.buttonIcon, onClick: function () {
                    opts.onClickButton.call(target);
                }
            });
        }
        setDisabled(target, opts.disabled);
        setReadonly(target, opts.readonly);
    };
    function destroy(target) {
        var tb = $.data(target, "textbox").textbox;
        tb.find(".textbox-text").validatebox("destroy");
        tb.remove();
        $(target).remove();
    };
    function setSize(target, width) {
        var state = $.data(target, "textbox");
        var opts = state.options;
        var tb = state.textbox;
        var parent = tb.parent();
        if (width) {
            opts.width = width;
        }
        if (isNaN(parseInt(opts.width))) {
            var c = $(target).clone();
            c.css("visibility", "hidden");
            c.insertAfter(target);
            opts.width = c.outerWidth();
            c.remove();
        }
        tb.appendTo("body");
        var text = tb.find(".textbox-text");
        var btn = tb.find(".textbox-button");
        var addon = tb.find(".textbox-addon");
        var icon = addon.find(".textbox-icon");
        tb._size(opts, parent);
        btn.linkbutton("resize", { height: tb.height() });
        btn.css({ left: (opts.buttonAlign == "left" ? 0 : ""), right: (opts.buttonAlign == "right" ? 0 : "") });
        addon.css({ left: (opts.iconAlign == "left" ? (opts.buttonAlign == "left" ? btn._outerWidth() : 0) : ""), right: (opts.iconAlign == "right" ? (opts.buttonAlign == "right" ? btn._outerWidth() : 0) : "") });
        icon.css({ width: opts.iconWidth + "px", height: tb.height() + "px" });
        text.css({ paddingLeft: (target.style.paddingLeft || ""), paddingRight: (target.style.paddingRight || ""), marginLeft: getMarginValue("left"), marginRight: getMarginValue("right") });
        if (opts.multiline) {
            text.css({ paddingTop: (target.style.paddingTop || ""), paddingBottom: (target.style.paddingBottom || "") });
            text._outerHeight(tb.height());
        } else {
            var height = Math.floor((tb.height() - text.height()) / 2);
            text.css({ paddingTop: height + "px", paddingBottom: height + "px" });
        }
        text._outerWidth(tb.width() - icon.length * opts.iconWidth - btn._outerWidth());
        tb.insertAfter(target);
        opts.onResize.call(target, opts.width, opts.height);
        function getMarginValue(align) {
            return (opts.iconAlign == align ? addon._outerWidth() : 0) + (opts.buttonAlign == align ? btn._outerWidth() : 0);
        };
    };
    function initializeValidate(target) {
        var opts = $(target).textbox("options");
        var state = $(target).textbox("textbox");
        state.validatebox($.extend({}, opts, {
            deltaX: $(target).textbox("getTipX"),
            onBeforeValidate: function () {
                var box = $(this);
                if (!box.is(":focus")) {
                    opts.oldInputValue = box.val();
                    box.val(opts.value);
                }
            },
            onValidate: function (valid) {
                var box = $(this);
                if (opts.oldInputValue != undefined) {
                    box.val(opts.oldInputValue);
                    opts.oldInputValue = undefined;
                }
                var tb = box.parent();
                if (valid) {
                    tb.removeClass("textbox-invalid");
                } else {
                    tb.addClass("textbox-invalid");
                }
            }
        }));
    };
    function setState(target) {
        var state = $.data(target, "textbox");
        var opts = state.options;
        var tb = state.textbox;
        var text = tb.find(".textbox-text");
        text.attr("placeholder", opts.prompt);
        text.unbind(".textbox");
        if (!opts.disabled && !opts.readonly) {
            text.bind("blur.textbox", function (e) {
                if (!tb.hasClass("textbox-focused")) {
                    return;
                }
                opts.value = $(this).val();
                if (opts.value == "") {
                    $(this).val(opts.prompt).addClass("textbox-prompt");
                } else {
                    $(this).removeClass("textbox-prompt");
                }
                tb.removeClass("textbox-focused");
            }).bind("focus.textbox", function (e) {
                if ($(this).val() != opts.value) {
                    $(this).val(opts.value);
                }
                $(this).removeClass("textbox-prompt");
                tb.addClass("textbox-focused");
            });
            for (var eventName in opts.inputEvents) {
                text.bind(eventName + ".textbox", { target: target }, opts.inputEvents[eventName]);
            }
        }
        var addon = tb.find(".textbox-addon");
        addon.unbind().bind("click", { target: target }, function (e) {
            var icons = $(e.target).closest("a.textbox-icon:not(.textbox-icon-disabled)");
            if (icons.length) {
                var index = parseInt(icons.attr("icon-index"));
                var icon = opts.icons[index];
                if (icon && icon.handler) {
                    icon.handler.call(icons[0], e);
                    opts.onClickIcon.call(target, index);
                }
            }
        });
        addon.find(".textbox-icon").each(function (index) {
            var icon = opts.icons[index];
            var iconItem = $(this);
            if (!icon || icon.disabled || opts.disabled || opts.readonly) {
                iconItem.addClass("textbox-icon-disabled");
            } else {
                iconItem.removeClass("textbox-icon-disabled");
            }
        });
        tb.find(".textbox-button").linkbutton((opts.disabled || opts.readonly) ? "disable" : "enable");
    };
    function setDisabled(target, disabled) {
        var state = $.data(target, "textbox");
        var opts = state.options;
        var tb = state.textbox;
        if (disabled) {
            opts.disabled = true;
            $(target).attr("disabled", "disabled");
            tb.find(".textbox-text,.textbox-value").attr("disabled", "disabled");
        } else {
            opts.disabled = false;
            $(target).removeAttr("disabled");
            tb.find(".textbox-text,.textbox-value").removeAttr("disabled");
        }
    };
    function setReadonly(target, readonly) {
        var state = $.data(target, "textbox");
        var opts = state.options;
        opts.readonly = readonly == undefined ? true : readonly;
        var text = state.textbox.find(".textbox-text");
        text.removeAttr("readonly").removeClass("textbox-text-readonly");
        if (opts.readonly || !opts.editable) {
            text.attr("readonly", "readonly").addClass("textbox-text-readonly");
        }
    };
    $.fn.textbox = function (options, param) {
        if (typeof options == "string") {
            var method = $.fn.textbox.methods[options];
            if (method) {
                return method(this, param);
            } else {
                return this.each(function () {
                    var box = $(this).textbox("textbox");
                    box.validatebox(options, param);
                });
            }
        }
        options = options || {};
        return this.each(function () {
            var state = $.data(this, "textbox");
            if (state) {
                $.extend(state.options, options);
                if (options.value != undefined) {
                    state.options.originalValue = options.value;
                }
            } else {
                state = $.data(this, "textbox", {
                    options: $.extend({}, $.fn.textbox.defaults, $.fn.textbox.parseOptions(this), options),
                    textbox: initialize(this)
                });
                state.options.originalValue = state.options.value;
            }
            initIcons(this);
            setState(this);
            setSize(this);
            initializeValidate(this);
            $(this).textbox("initValue", state.options.value);
        });
    };
    $.fn.textbox.methods = {
        options: function (jq) {
            return $.data(jq[0], "textbox").options;
        },
        textbox: function (jq) {
            return $.data(jq[0], "textbox").textbox.find(".textbox-text");
        },
        button: function (jq) {
            return $.data(jq[0], "textbox").textbox.find(".textbox-button");
        },
        destroy: function (jq) {
            return jq.each(function () {
                destroy(this);
            });
        },
        resize: function (jq, width) {
            return jq.each(function () {
                setSize(this, width);
            });
        },
        disable: function (jq) {
            return jq.each(function () {
                setDisabled(this, true);
                setState(this);
            });
        },
        enable: function (jq) {
            return jq.each(function () {
                setDisabled(this, false);
                setState(this);
            });
        },
        readonly: function (jq, readonly) {
            return jq.each(function () {
                setReadonly(this, readonly);
                setState(this);
            });
        },
        isValid: function (jq) {
            return jq.textbox("textbox").validatebox("isValid");
        },
        clear: function (jq) {
            return jq.each(function () {
                $(this).textbox("setValue", "");
            });
        },
        setText: function (jq, _3d) {
            return jq.each(function () {
                var _3e = $(this).textbox("options");
                var _3f = $(this).textbox("textbox");
                if ($(this).textbox("getText") != _3d) {
                    _3e.value = _3d;
                    _3f.val(_3d);
                }
                if (!_3f.is(":focus")) {
                    if (_3d) {
                        _3f.removeClass("textbox-prompt");
                    } else {
                        _3f.val(_3e.prompt).addClass("textbox-prompt");
                    }
                }
                $(this).textbox("validate");
            });
        },
        initValue: function (jq, _40) {
            return jq.each(function () {
                var _41 = $.data(this, "textbox");
                _41.options.value = "";
                $(this).textbox("setText", _40);
                _41.textbox.find(".textbox-value").val(_40);
                $(this).val(_40);
            });
        },
        setValue: function (jq, _42) {
            return jq.each(function () {
                var _43 = $.data(this, "textbox").options;
                var _44 = $(this).textbox("getValue");
                $(this).textbox("initValue", _42);
                if (_44 != _42) {
                    _43.onChange.call(this, _42, _44);
                }
            });
        },
        getText: function (jq) {
            var _45 = jq.textbox("textbox");
            if (_45.is(":focus")) {
                return _45.val();
            } else {
                return jq.textbox("options").value;
            }
        },
        getValue: function (jq) {
            return jq.data("textbox").textbox.find(".textbox-value").val();
        },
        reset: function (jq) {
            return jq.each(function () {
                var opts = $(this).textbox("options");
                $(this).textbox("setValue", opts.originalValue);
            });
        },
        getIcon: function (jq, _47) {
            return jq.data("textbox").textbox.find(".textbox-icon:eq(" + _47 + ")");
        },
        getTipX: function (jq) {
            var _48 = jq.data("textbox");
            var _49 = _48.options;
            var tb = _48.textbox;
            var _4a = tb.find(".textbox-text");
            var _4b = tb.find(".textbox-addon")._outerWidth();
            var _4c = tb.find(".textbox-button")._outerWidth();
            if (_49.tipPosition == "right") {
                return (_49.iconAlign == "right" ? _4b : 0) + (_49.buttonAlign == "right" ? _4c : 0) + 1;
            } else {
                if (_49.tipPosition == "left") {
                    return (_49.iconAlign == "left" ? -_4b : 0) + (_49.buttonAlign == "left" ? -_4c : 0) - 1;
                } else {
                    return _4b / 2 * (_49.iconAlign == "right" ? 1 : -1);
                }
            }
        }
    };
    $.fn.textbox.parseOptions = function (target) {
        var t = $(target);
        return $.extend(
            {},
            $.fn.validatebox.parseOptions(target),
            $.parser.parseOptions(target, [
                "prompt", "iconCls", "iconAlign", "buttonText", "buttonIcon", "buttonAlign",
                { multiline: "boolean", editable: "boolean", iconWidth: "number" }
            ]),
            {
                value: (t.val() || undefined),
                type: (t.attr("type") ? t.attr("type") : undefined),
                disabled: (t.attr("disabled") ? true : undefined),
                readonly: (t.attr("readonly") ? true : undefined)
            });
    };
    $.fn.textbox.defaults = $.extend({}, $.fn.validatebox.defaults, {
        width: "auto", height: 22, prompt: "", value: "", type: "text",
        multiline: false, editable: true, disabled: false, readonly: false,
        icons: [], iconCls: null,
        iconAlign: "right", iconWidth: 18,
        buttonText: "", buttonIcon: null, buttonAlign: "right",
        inputEvents: {
            blur: function (e) {
                var t = $(e.data.target);
                var opts = t.textbox("options");
                t.textbox("setValue", opts.value);
            }
        },
        onChange: function (newValue, oldValue) { },
        onResize: function (width, height) { },
        onClickButton: function () { },
        onClickIcon: function (index) { }
    });
})(jQuery);

